var Group = function(){
	return {
	    init: function () {

	    	//添加群组
	    	$(document).on('click','#group_submit_btn',function(){
	    		
	    		var name = $('#groupName').val();
	    		var desc = $('#groupDesc').val();

	    		if(name) {
	    			$.ajax({
	    				url: 'user/group_add',
	    				type: 'POST',
	    				dataType: 'json',
	    				data: {name: name,desc:desc},
	    			})
	    			.done(function(result) {
	    				if(result.code == 0) {
	    					toastr.success("添加成功");
	    				} else {
	    					toastr.error(result.msg);
	    				}
	    				$('#group_add_modal').modal("hide");
	    			})
	    			.fail(function() {
	    				console.log("error");
	    			})
	    			.always(function() {
	    				console.log("complete");
	    			});
	    			
	    		} else {
	    			toastr.warn('群组名称不能为空');
	    		}
	    	});

	    	//editable
	    	$("#name,#description").editable({
				url: 'user/group_update',
				ajaxOptions: {
			        dataType: 'json'
			    },
				success:function(response,newValue){
					console.log(response);
					if(response.code==1) 
					{return response.msg;} 
				}
			});

			//删除群组
			$(document).on('click','.group_del_btn',function(){
				var title = $(this).attr('data-title');
				var id = $(this).attr('data-pk');
				var tr = $(this).parents('tr');
				  bootbox.confirm("确认删除群组 <strong>"+title+"</strong>？", function(result) {
				  	if(result)
				  	{
                		$.ajax({
                			url: 'user/group_delete',
                			type: 'POST',
                			dataType: 'json',
                			data: {id: id},
                		})
                		.done(function(response) {
                			if(response.code === 0)
                			{
                				toastr.success(response.msg);
                				tr.remove();
                			} else {
                				toastr.error(response.msg);
                			}
                		})
                		.fail(function() {
                			console.log("error");
                		})
                		.always(function() {
                			console.log("complete");
                		});
                		
                	}
            	});  
			});

			// //jstree
			// $("#power-tree").jstree({
			//     "checkbox" : {
			//       "keep_selected_style" : false
			//     },
			//     "plugins" : [ "checkbox" ]
			//   });

			//全选
			$(document).on('click', '#checkall', function() {
				var checked = $(this).is(":checked");
				$.each($("#module_table tbody input[type='checkbox']"), function(index, val) {
					$.uniform.update($(val).attr("checked", checked)); 
				});
			});   
			
			//父模块
			$(document).on('click', '.module', function() {
				var id = $(this).attr('id');
				var checked = $(this).is(":checked");
				if(id)
				{
					$.each($('.'+id), function(index, val) {
						$.uniform.update($(val).attr("checked", checked)); 
					});
				}
			});

			//子模块
			$(document).on('click', '.module_child', function() {
				var id = $(this).attr('id');
				var father = $(this).attr('class').split(" ")[1];
				var checked = $(this).is(":checked");

				if(id)
				{
					$.each($('.'+id), function(index, val) {
						$.uniform.update($(val).attr("checked", checked)); 
					});

					if(checked)
					{
						if(!$('#'+father).is(":checked"))
						{
							$.uniform.update($('#'+father).attr("checked", true));
						} 
					}									
				}
			});

			//动作
			$(document).on('click', '.module_child', function() {
				var id = $(this).attr('id');
				var child = $(this).attr('class').split(" ")[2];
				var father = $(this).attr('class').split(" ")[1];
				var checked = $(this).is(":checked");

				if(id&&checked)
				{
					$('#'+child).is(":checked")
					{
						$.uniform.update($('#'+child).attr("checked", true)); 
					}

					$('#'+father).is(":checked")
					{
						$.uniform.update($('#'+father).attr("checked", true)); 
					}									
				}
			});
	    }
	};
}();   